//生成修改单据状态任务
function dj_xgzt(steps,datatable_id,dataId,zt){
    steps.push(
      {
        "step_name": "更新单据状态",
        "opt_type": 4,
        "datatable_id": datatable_id,
        "args": dataId?dataId:"提交表单",//如果dataid是空的话，那么要从提交表单步骤获取id
        "args_from": dataId?1:2,//如果dataid是空的话，那么传参是变量
        "field_params": [
          {
            "value_from": 1,
            "field": "zt",
            "value": zt,
            "data_type": 1
          }
        ]
      }
    )
    return steps;
  }
  //生成修改字段值任务(jobname步骤名称，steps步骤，datatable_id数据源id，dataId数据id，fields数组{"field":XXX,"value":XXX})
  function dj_xgz(steps,jobname,datatable_id,dataId,fields){
    var rw={
      "step_name": jobname,
      "opt_type": 4,
      "datatable_id": datatable_id,
      "args": dataId?dataId:"提交表单",//如果dataid是空的话，那么要从提交表单步骤获取id
      "args_from": dataId?1:2,//如果dataid是空的话，那么传参是变量
      "field_params": [
      ]
    }
    fields.forEach(item => {
      rw.field_params.push({
        "value_from": item.value=="提交表单"?2:1,//如果值是提交表单，那么是变量，枚举是2
        "field": item.field,
        "value": item.value,
        "data_type": 1
      })
    });
    steps.push(rw);
    return steps;
  }
  //生成修改字段值任务，可设置value_from(jobname步骤名称，steps步骤，datatable_id数据源id，dataId数据id，fields数组{"field":XXX,"value":XXX,"value_from":XXX})
  function dj_xgz_plus(steps,jobname,datatable_id,dataId,fields){
    var rw={
      "step_name": jobname,
      "opt_type": 4,
      "datatable_id": datatable_id,
      "args": dataId?dataId:"提交表单",//如果dataid是空的话，那么要从提交表单步骤获取id
      "args_from": dataId?1:2,//如果dataid是空的话，那么传参是变量
      "field_params": [
      ]
    }
    fields.forEach(item => {
      rw.field_params.push({
        "value_from": item.value_from,
        "field": item.field,
        "value": item.value,
        "data_type": 1
      })
    });
    steps.push(rw);
    return steps;
  }
  //状态提示方法，根据状态进行判断，如果不是传入的状态则弹出提示，如果是传入的状态则执行方法,dqzt当前状态，zt进行对比的状态，ts提示文本，func执行的方法
  function dj_ztts(dqzt,zt,ts,func){
    if(dqzt==zt){
        func();
    }else{
        ycWindow.dom.opener.dom.showMsg(ts, "error")
    }
  }
  //新增数据任务(steps步骤数组,jobname步骤名称,datatable_id数据源id,fields字段数组{"field":XXX,"value":XXX})
  function dj_xzsj(steps,jobname,datatable_id,fields){
    var rw={
      "step_name": jobname,
      "opt_type": 1,
      "datatable_id": datatable_id,
      "args": "",
      "field_params": []
    }
    fields.forEach((item,index) => {
      rw.field_params.push({
        "value_from": item.value=="提交表单"?2:1,//如果值是提交表单，那么是变量，枚举是2
        "field": item.field,
        "value": item.value,
        "data_type": 1
      })
    });
    steps.push(rw);
    return steps;
  }
  //新增数据可设置value_from(steps步骤数组,jobname步骤名称,datatable_id数据源id,id数据id（可以自己赋值，为空就是自动生成）,fields字段数组{"field":XXX,"value":XXX,"value_from":XXX})
  function dj_xzsj_plus(steps,jobname,datatable_id,id,fields){
    var rw={
      "step_name": jobname,
      "opt_type": 1,
      "datatable_id": datatable_id,
      "args": id,
      "field_params": []
    }
    fields.forEach((item,index) => {
      rw.field_params.push({
        "value_from": item.value_from,
        "field": item.field,
        "value": item.value,
        "data_type": 1
      })
    });
    steps.push(rw);
    return steps;
  }
  //单据和事务一起提交
  function dj_submitWithJobs(submitStepName, submitIndexOfSteps, submitReplaces, jobArgs, success, fails){
    if(jobArgs.steps&&jobArgs.steps.length){
      ycWindow.plugins.form.submitWithJobs(submitStepName, submitIndexOfSteps, submitReplaces, jobArgs,success, fails)
    }else{
      ycWindow.plugins.form.submit(success, fails)
    }
  }
  //批量删除数据源所有数据(dts数据源id数组)
  function dj_plscsysj(dts){
    dts.forEach(dt => {
      //查询所有数据
      const args = {
        "datatable_id": dt,
        "search": "",
        "search_fields": [
        ],
        "advance_filters": {condition: "", values: JSON.stringify([])},
        "page": 1,
        "page_size": 999999,
      }
      ycWindow.plugins.crud.Query(args, (result) => {
        //记录所有数据id
        var ids=[];
        result.data.forEach(item => {
          ids.push(item.id);
        });
        if(ids.length){
            //删除所有数据
            const args2 = {
                "datatable_id": dt,
                "ids": ids,
            }
            ycWindow.plugins.crud.BatchDelete(args2, (data) => {
            }, (data) => {
                // 失败
            })
        }
      }, (data) => {
      })
    });
  }
  //生成删除数据任务
  function dj_scsj(steps,jobname,datatable_id,dataId){
    steps.push(
      {
        "step_name": jobname,
        "opt_type": 2,
        "datatable_id": datatable_id,
        "args": dataId, // 删除时这里是数据id，批量删除多个id逗号分隔
        "field_params": []
    }
    )
    return steps;
  }
  //生成批量删除数据任务
  function dj_plscsj(steps,jobname,datatable_id,dataId){
    steps.push(
      {
        "step_name": jobname,
        "opt_type": 3,
        "datatable_id": datatable_id,
        "args": dataId, // 删除时这里是数据id，批量删除多个id逗号分隔
        "field_params": []
    }
    )
    return steps;
  }
  //生成更新单据映射更新数据步骤
  function dj_djysgx(steps,jobname,database_id,dataId,objs,dom){
    var step={
      "step_name": jobname,
      "opt_type": 4,
      "datatable_id": database_id,
      "args": dataId,
      "args_from": 1,
      "field_params": [
      ]
    }
    objs.forEach(item => {
      var value=dom.getControlByName(item.control).getValue();
      step.field_params.push(      {
        "value_from": 1,
        "field": item.field,
        "value": value?value:"",
        "data_type": 1
      })
    });
    steps.push(step)
    return steps
  }
  //更新字段值(datatable_id数据源id，data_id数据id，fields[{name:字段名,value:值},...]，success成功回调，fails失败回调)
  function dj_update(datatable_id,data_id,fields,success,fails){
    var args={
      "datatable_id":datatable_id,
      "data_id":data_id,
      "form_items":{
      }
    }
    fields.forEach(item => {
      args.form_items[item.name]={
        "value":item.value,
        "type":1,
        "args":{}
      }
    });
    ycWindow.plugins.crud.Update(args,success,fails)
  }